package com.jxd.petMgr.servlet;

import com.jxd.petMgr.service.IPetService;
import com.jxd.petMgr.service.impl.PetServiceImpl;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/getTypeNameServlet")
public class GetTypeNameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        IPetService petService = new PetServiceImpl();
        //因为前台以访问事件作为servlet的触发事件如果使用转发或重定向，就会导致无线循环
        //使用前台Ajax后台响应流的方式
        PrintWriter pw = response.getWriter();
        List<String> list = petService.getAllTypeName();
        JSONArray jsonArray = JSONArray.fromObject(list);
        String str = jsonArray.toString();
        pw.write(jsonArray.toString());
        pw.flush();
        pw.close();
    }
}
